package com.xy.demo.Test;

import com.alibaba.fastjson.JSON;
import com.xy.demo.Util.Util;
import com.xy.demo.model.User;
import com.xy.demo.model.Userm;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args) {
        List<User> list = new ArrayList<User>();
        for(int i = 1 ; i<5 ;i++){
           User user = new User();
           String userName = "Yang"+i;
           String passWord = "word"+i;
           int age = i;
           double money = i ;
           BigDecimal sub = new BigDecimal(i);
           user.setUsername(userName);
           user.setPassword(passWord);
           user.setAge(age);
           user.setMoney(money);user.setSubtotal(sub);
           list.add(user);
        }
        User user = new User();
        user.setUsername("Yang1");
        user.setPassword("word1");
        user.setAge(22);
        user.setMoney(100.00);
        user.setSubtotal(new BigDecimal(20));
        list.add(user);
        printList(list,"1.原始数据");

        // 根据用户名 去重 并求和money   三个参数  User::getUsername map的key o->o(User实体类) map的val  (o1,o2)是key重复时的处理函数
        List<User> listDistinctAndSum = list.stream().collect(Collectors.toMap(s->s.getUsername()+";"+ Util.toString(s.getPassword()), o->o,(o1, o2)->{
            o1.setMoney(o1.getMoney()+o2.getMoney());
            o1.setSubtotal(Util.add(o1.getSubtotal(),o2.getSubtotal()));
            return o1;
        })).values().stream().collect(Collectors.toList());
        printList(listDistinctAndSum,"5.根据用户名去重 并求和money");

        // stream
        List<User> list1 = list.stream().filter(o->o.getUsername().equals("Yang1")).collect(Collectors.toList());
        printList(list1,"2.筛选username=Yang1");

        // map分类
        Map<String,List<User>> map = list.stream().collect(Collectors.groupingBy(User::getUsername));


        //现根据是否通过考试对学生分组，在根据性别分组
        Map<String,Map<String,List<User>>>  map1 =   list.stream().collect(Collectors.groupingBy(User::getUsername, Collectors.groupingBy(User::getPassword)));

        int a = 1;

        // 去重 username 条件去重
        List<User> listDistinct1 = list.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o->o.getUsername()))),ArrayList::new)
        );
        printList(listDistinct1,"3.根据username去重");

       // 去重 username 和 password 两个条件去重
        List<User> listDistinct2 = list.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o->o.getUsername()+";"+o.getPassword()))),ArrayList::new)
        );
        printList(listDistinct2,"4.两个条件username/password去重");


        //排序
        List<User> listSortedT = list.stream().sorted(Comparator.comparing(s->s.getAge())).collect(Collectors.toList());
        List<User> listSortedF = list.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
        List<User> listSortedFF = list.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getMoney)).collect(Collectors.toList());
        printList(listSortedT,"6.年龄排序正序(可以看到初始数据钱：从1-->101,也就是 第五条求和money时,重新setMoney,这里要谨慎使用,修改了原始数据)");
        printList(listSortedF,"7.年龄排序负序");
        printList(listSortedFF,"8.年龄排序负序,然后钱正序");

        //求和 最大值 最小值  平均值
        long ageSum = list.stream().mapToLong(User::getAge).sum();
        long ageMin = list.stream().mapToLong(User::getAge).min().getAsLong();
        long ageMax = list.stream().mapToLong(User::getAge).max().getAsLong();
        double ageAverage = list.stream().mapToLong(User::getAge).average().getAsDouble();
        //
        BigDecimal sumBigDecimal = list.stream().map(o->o.getSubtotal()).reduce(BigDecimal.ZERO,BigDecimal::add);
        System.out.println("9.年龄求和："+ageSum);
        System.out.println("10.年龄最大："+ageMax);
        System.out.println("11.年龄最小："+ageMin);
        System.out.println("12.年龄平均值："+ageAverage);
        System.out.println("12.subtotal："+sumBigDecimal);

        //姓名拼接
        String  usernameJoin = list.stream().map(User::getUsername).collect(Collectors.joining(","));
        System.out.println("13.姓名拼接："+usernameJoin);


        // 新集合 转化成另一个对象的集合
        List<String>  usernameList = list.stream().map(User::getUsername).collect(Collectors.toList());
        System.out.println("14.新集合：================");
        usernameList.forEach(s-> System.out.println("username:"+s));
        List<Userm> userms = list.stream().map(e-> new Userm(e.getUsername(),e.getPassword())).collect(Collectors.toList());
        printListUs(userms,"User->转换成-->Userm");

        int b = 2;

    }

    public static void printList(List<User> list,String memo){
        if(list.size()>0){
            System.out.println();
            System.out.println(memo+"===============");
            for(User u:list){
                System.out.println("用户名："+u.getUsername()+"--->密码："+u.getPassword()+"--->年龄："+u.getAge()+"--->钱："+u.getMoney()+"--->subtotal:"+u.getSubtotal());
            }
        }
    }

    public static void printListUs(List<Userm> list,String memo){
        if(list.size()>0){
            System.out.println();
            System.out.println(memo+"===============");
            for(Userm u:list){
                System.out.println("用户名"+u.getUsername()+"--->密码"+u.getPassword());
            }
        }
    }


}
